//
//  SLEmoticonPageCell.swift
//  Emotion
//
//  Created by lirenqiang on 16/6/1.
//  Copyright © 2016年 lirenqiang. All rights reserved.
//

import UIKit

class SLEmoticonPageCell: UICollectionViewCell {
    
    var buttons: [SLEmoticonButton] = [SLEmoticonButton]()
    
    //获取每页的表情数组
    var emoticons: [SLEmoticon]?  {
        didSet {
            //绑定数据
            //获取所有的按钮
            //给所有的按钮设置模型数据
            for (index, emoticon) in emoticons!.enumerate() {
                
                let btn = buttons[index]
                
                btn.emoticon = emoticon
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.contentView.backgroundColor = randomColor()
        
        setupUI()
    }
    
    //MARK: - 设置界面
    private func setupUI() {
        
        //表情键盘两边的间距
        let margin: CGFloat = 5
        //距离底部的间距
        let bottomMargin: CGFloat = 30
        
        //求出每个按钮的尺寸
        let btnW = (self.bounds.width - (2 * margin)) / CGFloat(EmoticonColCount)
        let btnH = (self.bounds.height - bottomMargin) / CGFloat(EmoticonRowCount)
        
        for i in 0..<EmoticonPageCount {
            
            let col = i % EmoticonColCount
            let row = i / EmoticonColCount
            
            let x = margin + (btnW * CGFloat(col))
            let y = btnH * CGFloat(row)
            
            let rect = CGRect(x: x, y: y, width: btnW, height: btnH)
            
            let btn = SLEmoticonButton(frame: rect)
            btn.addTarget(self, action: #selector(SLEmoticonPageCell.btnDidClick(_:)), forControlEvents: .TouchUpInside)
            
//            btn.setTitle("王大锤", forState: .Normal)
            
            btn.titleLabel?.font = UIFont.systemFontOfSize(10)
            
            buttons.append(btn)
            
            contentView.addSubview(btn)
        }
        
        //MARK: 添加手势
        let longPress = UILongPressGestureRecognizer(target: self, action: #selector(SLEmoticonPageCell.longPressFire(_:)))

        self.addGestureRecognizer(longPress)
    }
    
    @objc private func btnDidClick(btn: SLEmoticonButton) {
        
        NSNotificationCenter.defaultCenter().postNotificationName(EmoticonBtnDidClickNotification, object: btn.emoticon)
        
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 根据手势触摸的点,获取对应的按钮
    private func buttonWithPoint(point: CGPoint) -> SLEmoticonButton? {
        
        for btn in buttons {
            //根据按钮的frame 是否包含了触摸点
            if btn.frame.contains(point) {
                return btn
            }
        }
        return nil
    }
    
    @objc private func longPressFire(ges: UILongPressGestureRecognizer) {
        let point = ges.locationInView(contentView)
        
        //根据手势触摸的店,找到对应的按钮
        guard let btn = buttonWithPoint(point) else {
            popView.dismissAfter()
            return 
        }
        
        //如果手势在  删除或者空白的按钮比上不添加pop
        if btn.emoticon!.isDelete || btn.emoticon!.isEmpty {
            
            popView.dismissAfter()
            return 
        }
        
        
        switch ges.state {
        case .Began, .Changed:
            popView.show(btn)
            
        default:
            popView.dismissAfter()
        }
        
    }
    
    private lazy var popView: SLEmoticonPopView = {
       
        let pop = SLEmoticonPopView.loadEmoticonPopView()
        
        return pop
        
    }()
    
}

































